<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <button>改变文本</button>
    </div>
    <!-- 注意: 部署时，将 "development.js" 替换为 "production.min.js"。-->
    <!-- jsx特点：多个标签最外层（根）只能有一个标签 -->
    <script
      src="https://unpkg.com/react@16/umd/react.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
      crossorigin
    ></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

    <script type="text/babel">
      // 封装APP组件
      class App extends React.Component {
        constructor() {
          super();
          // this.message='Hello World'
          this.state = {
            message: "Hello World",
            counter:0
          };
          this.btnClick = this.btnClick.bind(this);
        }
        render() {
          const { message,counter } = this.state;
          return (
            <div>
              {/*方案1bind绑定this显示绑定*/}
              <h2>{this.state.message}</h2>
              <button onClick={this.btnClick}>改变文本</button>
              <button onClick={this.btnClick}>改变文本</button>
              <button onClick={this.btnClick}>改变文本</button>
              <h2>counter:{counter}</h2>
              {/*方案2：定义含糊是使用箭筒函数*/}
              <button onClick={this.increment}>+1</button>
              {/*方案3（推荐）：直接传入箭头函数，直接在箭头函数中执行函数*/}
              <button onClick={()=>{
                this.decrement("why")
              }}>-1</button>
            </div>
          );
        }
        btnClick() {
          // console.log('按钮发生了点击',this.state.message);
          // this.state.message = "Hello React"
          this.setState({
            message: "Hello React",
          });
        }
        increment = () => {
          this.setState({
            counter:this.state.counter + 1
          })
        };
        decrement = (name) => {
          console.log(name);
          this.setState({
            counter:this.state.counter - 1
          })
        };
      }
      // 渲染组件
      ReactDOM.render(<App />, document.getElementById("app"));
    </script>
  </body>
</html>
